import axios from 'axios';

axios.interceptors.request.use(config => {
  // loading
    /*let _data = await http('post', '/checkToken');
    console.log(_data)*/
    return config
}, error => {
    return Promise.reject(error)
})

axios.interceptors.response.use(response => {
    return response
}, error => {
    return Promise.resolve(error.response)
})

const http = (method, url, data) => {
    let config = {
        method,
        // baseURL: 'http://localhost:61110',
        baseURL: 'http://www.bestzsyl.cn:61110',
        url,
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
            'Content-Type': 'application/json; charset=UTF-8',
            'Access-Token': localStorage.getItem('token')
        }
    }
    if(method === 'post') {
        config.data = data || {};
    }else if (method === 'get') {
        config.params = data || {};
    }

    return new Promise((resolve, reject) => {
        axios(config)
        .then(response => {
            resolve(response)
            // return response
        })
        .catch(err => {
            console.log(err)
            reject(err)
        })
    })
}
export default http;